/* 消息中心 */
<template>
    <div style="background: #f8f8f9">
        <Row :gutter="32">
            <!-- 左侧 -->
            <Col span="17">
                <Card>
                    <p slot="title">收件箱</p>
                    <Table @on-row-click="showMod" border :columns="columns1" :data="messageToMe" @on-selection-change="fromOtherCheck" :loading="fromOtherLoad"></Table>
                    <Row type="flex" justify="space-between" style="margin-top: 10px">
                        <Col span="6" style="margin-top: 10px">
                            <Button type="error" @click="deleteFromOther">删除</Button>
                            <Button type="primary" @click="lookOk">已查看</Button>
                        </Col>
                        <Col span="18" style="text-align:right">
                            <Page :total="fromCount" show-total @on-change="fromChange" />
                        </Col>
                    </Row>
                </Card>
                <Card style="margin-top: 10px">
                    <p slot="title">发件箱</p>
                    <Table border :columns="columns2" :data="toOther" @on-row-click="showTother" @on-selection-change="toOtherCheck" :loading="toOtherLoad"></Table>
                    <Row type="flex" justify="space-between" style="margin-top: 10px">
                        <Col span="8">
                            <Button type="error" @click="deleteToOther">删除</Button>
                        </Col>
                        <Col span="12" style="text-align:right">
                            <Page :total="toCount" show-total @on-change="toChange" />
                        </Col>
                    </Row>
                </Card>
            </Col>
            <!-- 右侧 -->
            <Col span="7">
                <Card style="height: 1000px;overflow: hidden">
                    <p slot="title">联系人</p>
                    <Row>
                        <Col span="24">
                            <Select
                                    v-model="modelSelect"
                                    placeholder="请输入关键字"
                                    clearable
                                    filterable
                                    remote
                                    @on-change = "remoteSelect"
                                    :loading="loadingSelect">
                                <Option v-for="(option, index) in listSelect" :value="option.id" :key="index">{{option.realName}}</Option>
                            </Select>
                        </Col>
                    </Row>
                    <table width="100%" cellspacing="0" cellpadding="0" style="margin-top: 10px">
                        <tr>
                            <th>
                                联系人{{this.contacts}}人
                            </th>
                        </tr>
                        <tr>
                            <td class="leftbgc" style="text-align:left;">
                                <div style="height:750px;overflow-y: scroll">
                                    <Tree :data="data3" ref="tree4"  @on-select-change="getMemberInfo"></Tree><!--@on-toggle-expand="getCheckedNodes"-->
                                    <!-- <Tree :data="data3" ref="tree4" @on-check-change="choiceAll" ></Tree> -->
                                </div>
                            </td>
                        </tr>
                    </table>
                </Card>
            </Col>
        </Row>
        <Modal title="消息查看" v-model="modal7" :closable="false" :mask-closable='false' cancel-text=''>
            <Form :model="formLeft" label-position="left" :label-width="100">
                <FormItem label="发件人：" v-if="isSend">
                    {{infoData.user_id}}
                </FormItem>
                <FormItem label="收件人：" v-else>
                    {{infoData.user_id}}
                </FormItem>
                <FormItem label="主题：">
                    {{infoData.app_name}}
                </FormItem>
                <FormItem label="消息内容：">
                    {{infoData.content}}
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" @click="confirmOk">确定</Button>
            </div>
        </Modal>
        <Modal title="发消息" v-model="modalSend" :closable="false">
            <Form :model="formLeft" label-position="left" :label-width="100">
                <FormItem label="收件人：">
                    张三
                </FormItem>
                <FormItem label="主体：">
                    我发给李四
                </FormItem>
                <FormItem label="消息内容">
                    明天开会
                </FormItem>
            </Form>
        </Modal>
        <Modal title="信息查看" v-model="modalInfo" :closable="false">
            <Row :gutter="16">
                <Col span="12">
                    <Form :model="Contacts" label-position="left" :label-width="70">
                        <FormItem label="姓名">
                            {{persondata.RealName}}
                        </FormItem>
                        <FormItem label="性别">
                            <span v-if="persondata.Gender=='F'">女</span>
                            <span v-else>男</span>
                        </FormItem>
                        <FormItem label="职位">
                            {{persondata.Job}}
                        </FormItem>
                        <FormItem label="所属部门">
                            {{persondata.BranchInnerCode}}
                        </FormItem>
                        <FormItem label="手机号码">
                            {{persondata.Tel}}

                        </FormItem>
                    </Form>
                </Col>
                <Col span="12">
                    <Form :model="Contacts" label-position="left" :label-width="40">
                        <FormItem label="头像">
                            <div style="width:70px;height:70px;">
                                <img :src="persondata.LogoFile" style="width:70px;height:70px;"/>
                            </div>
                        </FormItem>
                        <!-- <FormItem label="生日">

                        </FormItem> -->
                        <FormItem label="邮箱">
                            {{persondata.Email}}

                        </FormItem>
                        <FormItem label="QQ">
                            {{persondata.QQ}}

                        </FormItem>
                        <FormItem label="微信">
                            {{persondata.Prop2}}
                        </FormItem>
                    </Form>
                </Col>
            </Row>
            <Button @click="sendInfo" type="info">发送消息</Button>
        </Modal>
        <Modal
                v-model="sendModel"
                title="发送消息"
                @on-ok="sendOk"
                @on-cancel="cancelOk">
            <Input v-model="mainText" type="text" :rows="4" placeholder="请输入主题..." />
            <Input v-model="sendVal" type="textarea" :rows="4" placeholder="请输入信息..." />
        </Modal>
    </div>
</template>

<script>

    export default {
        data() {
            return {
                mainText:'',
                loadingSelect:false,
                modelSelect:'',
                listSelect:[],
                modalInfo: false,
                modalSend: false,
                modal7: false,
                sendModel: false,
                sendVal: '',
                formLeft: {
                    input1: "",
                    input2: "",
                    input3: ""
                },
                Contacts: {
                },
                columns1: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "发件人",
                        key: "send_id",
                        align: "center",
                        render: (h, params) => {
                            return h('div', [
                                h('Icon',{ props: { type: params.row.has_read==0?'ios-mail':'ios-mail-open-outline',size: 16},}),
                                h('strong', { style: {  margin: '0 8px'},}, this.messageToMe[params.index].send_id),
                            ])
                        }
                    },
                    {
                        title: "主题",
                        key: "app_name",
                        align: "center"
                    },
                    {
                        title: "内容",
                        key: "content",
                        align: "center"
                    },
                    {
                        title: "时间",
                        key: "created_at",
                        align: "center"
                    }
                ],
                messageToMe: [],
                columns2: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center",
                    },
                    {
                        title: "收件人",
                        key: "type",
                        align: "center"
                    },
                    {
                        title: "主题",
                        key: "app_name",
                        align: "center"
                    },
                    {
                        title: "内容",
                        key: "content",
                        align: "center"
                    },
                    {
                        title: "时间",
                        key: "created_at",
                        align: "center"
                    }
                ],
                toOther: [],
                data3: [],
                searchName:'',
                toCount:0,
                toLimit:10,
                toPage:1,
                fromPage:1,
                fromLimit:10,
                fromCount:0.,
                persondata:{},       //树组件的个人信息
                memberId: '',
                infoData: {},
                contacts: '',
                toOtherIds:'',
                fromOtherIds:'',
                toOtherLoad:false,
                fromOtherLoad:false,
                isSend:true
            };
        },
        methods: {
            // 获取收件箱未读个数
            getMessageNum() {
                social.get('/unreadInfo').then(res=>{
                    this.$store.commit('setMessageNum',res.sumCount)
                })
            },
            // 收件箱点确定
            confirmOk() {
                this.modal7=false
            },
            showMod(info) {
                this.isSend = true;
                console.log(info);
                social({
                    method: 'get',
                    url: '/info/notice/'+info.id,

                }).then((res)=>{
                    axios.post("/branch/findName",{
                        Ids:info.data
                    }).then( respose => {
                        this.infoData = res.data
                        this.modal7 = true;
                        if(respose._STATUS==1){
                            if(respose.userName[0]){
                                this.infoData.user_id = respose.userName[0].name
                            }else{
                                this.infoData.user_id = '未知'
                            }
                        }else{
                            if(info.data == -1){
                                this.infoData.user_id = '系统消息'
                            }else{
                                this.infoData.user_id = '未知'
                            }
                        }
                        if(info.has_read==0) {
                            this.getMessageNum();
                            this.fromOther()
                        }
                    })
                }).catch((err)=>{
                    console.log(err)
                })
            },
            showTother(info) {
                this.isSend = false;
                social({
                    method: 'get',
                    url: '/info/notice/send_show/'+info.id,
                }).then((res)=>{
                    axios.post("/branch/findName",{
                        Ids:res.data.type
                    }).then( respose => {
                        if(respose._STATUS==1){
                            this.infoData = res.data
                            this.modal7 = true;
                            if(respose.userName[0]){
                                this.infoData.user_id = respose.userName[0].name
                            }else if(res.data.type == -1){
                                this.infoData.user_id = '系统消息'
                            }else{
                                this.infoData.user_id = '未知'
                            }
                        }else{
                            this.$Message.error("查询失败")
                        }
                    })
                }).catch((err)=>{
                    console.log(err)
                })
            },
            //树组件单个人的信息
            getMemberInfo(data){
                if(data[0] && data[0].companyType==4){
                    axios.post('/member/findByMemberId',{
                        Id:data[0].id
                    }).then(res=>{
                        console.log(res)
                        if(res.member.ID){
                            this.memberId = res.member.ID
                            this.persondata=res.member
                        }
                        if(res.member.LogoFile != null &&res.member.LogoFile != ''){
                            this.persondata.LogoFile = 'https://static.anhuanjia.com/avatar/'+ res.member.LogoFile + '?x-oss-process=image/resize,m_fill,h_96,w_96&' + new Date().getTime()
                        }else{
                            this.persondata.LogoFile ='https://static.anhuanjia.com/avatar/default.png?x-oss-process=image/resize,m_fill,h_96,w_96'
                        }
                    })
                    this.modalInfo = true;
                }
            },
            ok() {
                this.modalSend = true;
            },
            getInfo(page){
                console.log(page)
            },
            sendInfo(){
                this.sendModel = true
                console.log('sendInfo')
            },
            //发送消息
            sendOk(){
                var userId = parseInt(localStorage.getItem('uId'))
                social({
                    method: 'post',
                    url: '/info/notice',
                    params: {
                        content: this.sendVal,
                        app_name: this.mainText?this.mainText:'无主题',
                        app: 'eao',
                        type: this.memberId+'',
                        uid: userId
                    },
                }).then(res=>{
                    if(res.status == 1){
                        this.toOthers();
                        this.fromOther();
                        this.$Message.success(res.msg)
                        //成功后发送内容清空
                        this.sendVal = ''
                        this.mainText = ''
                    }else{
                        this.$Message.error(res.msg)
                    }
                }).catch(err=>{
                    this.$Message.error("发送失败")
                })
            },
            cancelOk(){this.$Message.info('取消发送...')},
            selbtn(selection){        //收件箱删除
                let delInfo = [];
                selection.map((i)=>{
                    delInfo.push(i.id)
                })
                console.log(delInfo)
            },
            deleteInfo(){
                this.selbtn()
            },
            //发件箱
            toOthers(){
                this.toOtherLoad = true;
                social({
                    method: 'get',
                    url: '/info/notice/get_send_list',
                    params: {
                        limit:this.toLimit,
                        page:this.toPage
                    },
                }).then(res=>{
                    let ids = '';
                    for(let i = 0;i<res.data.length;i++){
                        ids = ids + res.data[i].type + ','
                    }
                    axios.post("branch/findName",{
                        Ids:ids.substring(0,ids.length-1)
                    }).then( respose => {
                        for(let i = 0;i<res.data.length;i++){
                            let isTrue = false;
                            for(let j = 0;j<respose.userName.length;j++){
                                if(respose.userName[j].id==res.data[i].type){
                                    isTrue =true;
                                    res.data[i].type = respose.userName[j].name
                                    break;
                                }else if(res.data[i].type == 'system'){
                                    isTrue =true;
                                    res.data[i].type = '系统消息'
                                    break;
                                }
                            }
                            if(!isTrue){
                                res.data[i].type = '未知'
                            }
                        }
                        this.toCount = res.count;
                        this.toOther = res.data;
                        this.toOtherIds =''
                        this.toOtherLoad = false
                    })
                }).catch(err => {
                    if(this.toPage>1){
                        this.toPage = this.toPage-1;
                        this.toOthers();
                    }else{
                        this.toCount = 0;
                        this.toOther = [];
                        this.toOtherLoad = false
                    }
                })
            },
            toChange(value){
                this.toPage = value;
                this.toOthers();
            },
            //收件箱信息
            fromOther(){
                this.getMessageNum();
                this.fromOtherLoad = true;
                social({
                    method: 'get',
                    url: '/info/notice',
                    params: {
                        type:'self',
                        limit:this.fromLimit,
                        page:this.fromPage
                    },
                }).then((res)=>{
                    if(res.status==1) {
                        let ids='';
                        for(let i = 0;i<res.data.length;i++){
                            ids += res.data[i].send_id+','
                        }
                        axios.post("branch/findName",{
                            Ids:ids.substring(0,ids.length-1)
                        }).then( respose => {
                            console.log(respose)
                            for(let i = 0;i<res.data.length;i++){
                                res.data[i].data = res.data[i].send_id
                                let send = false;
                                for(let j = 0;j<respose.userName.length;j++){
                                    if(respose.userName[j].id == res.data[i].send_id){
                                        send = true;
                                        res.data[i].send_id = respose.userName[j].name
                                        break;
                                    }
                                }
                                if(res.data[i].send_id == -1){
                                    res.data[i].send_id = '系统消息'
                                    send =true;
                                }
                                if(!send){
                                    res.data[i].send_id = '未知';
                                }
                            }
                            this.messageToMe = res.data
                            this.fromCount = res.count
                            this.fromOtherIds = ''
                            this.fromOtherLoad = false
                        })
                    }else {
                        this.messageToMe=[]
                    }
                }).catch((err)=>{
                    if(this.fromPage>1){
                        this.fromPage = this.fromPage-1
                        this.fromOther();
                    }else{
                        this.messageToMe = []
                        this.fromCount = 0
                        this.fromOtherLoad = false
                    }
                })
            },
            fromChange(value){
                this.fromPage = value;
                this.fromOther();
            },
            likeBranchMember(){
                axios.post('/branch/likeBranchMember').then(res=>{
                    this.listSelect = res.selectMember;
                    this.contacts = res.selectMember.length + ''
                })
            },
            remoteSelect(value){
                if(!value) {
                    return
                }
                axios.post('/member/findByMemberId',{
                    Id: value
                }).then(res=>{
                    console.log(res)
                    if(res._STATUS==1){
                        this.memberId = res.member.ID
                        this.persondata=res.member
                        this.modalInfo = true
                    }else{
                        this.$Message.error(res._MESSAGE)
                    }
                }).catch((err)=>{
                    console.log(err)
                })
            },
            toOtherCheck(data){
                let idStr = '';
                for(let i = 0;i<data.length;i++){
                    idStr = idStr + data[i].id + ',';
                }
                this.toOtherIds  =  idStr.substring(0,idStr.length-1)
            },
            fromOtherCheck(data){
                let idStr = '';
                for(let i = 0;i<data.length;i++){
                    idStr = idStr + data[i].id + ',';
                }
                this.fromOtherIds  =  idStr.substring(0,idStr.length-1)
            },
            deleteToOther(){
                if(this.toOtherIds){
                    social({
                            method: 'post',
                            url: '/info/notice/del_many_send',
                            params: {
                                ids : this.toOtherIds,
                            },
                        }
                    ).then(res=>{
                        if(res.status==1){
                            this.$Message.success(res.msg)
                            this.toOthers();
                        }else{
                            this.$Message.error(res.msg)
                        }
                    })
                }else{
                    this.$Message.error('请选择要删除的信息')
                }
            },
            deleteFromOther(){
                if(this.fromOtherIds){
                    social({
                        method: 'post',
                        url: '/info/notice/del_all',
                        params: {
                            ids : this.fromOtherIds
                        },
                    }).then(res=>{
                        if(res.status==1){
                            this.$Message.success(res.msg)
                            this.fromOther();
                        }else{
                            this.$Message.error(res.msg)
                        }
                    })
                }else{
                    this.$Message.error('请选择要删除的信息')
                }
            },
            lookOk(){
                if(this.fromOtherIds){
                    social({
                        method: 'post',
                        url: '/info/notice/set_read',
                        params: {
                            ids: this.fromOtherIds
                        },
                    }).then((res)=>{
                        if(res.status){
                            this.$Message.info(res.msg)
                            this.fromOther()
                            this.getMessageNum();
                        }
                    }).catch(error => {
                        console.log(error)
                    })
                }else{
                    this.$Message.info('请选择消息')
                }
            }
        },
        mounted() {
            this.likeBranchMember();//下拉框人员
            this.toOthers();//发件箱
            this.fromOther();//收件箱
            // 获取树菜单列表
            this.$post('ProjectInfo.contactsMenu',{}).then((res)=>{
                for(let i=0;i<res.branchTree.length;i++){
                    res.branchTree[i].expand=true;
                    for(let j=0;j<res.branchTree[i].children.length;j++){
                        res.branchTree[i].children[j].expand=true;
                    }
                }
                this.data3=res.branchTree;
            }).catch((err)=>{
                console.log(err)
            });
        }
    };
</script>

<style lang="less" scoped>
    /deep/ .ivu-tree-title {
        font-size: 13px;
    }
</style>
